<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class Menu_model extends CI_Model {
	function catalog($out) {
		$list = $this->db->select()->from('catalog_parents')->where('parent_parent', '0')->where('parent_status', '1')->order_by('parent_order')->get()->result_array();
		$out['text'] = "<ul class='menu-items'>";
		//dump($out['data_from_model']['catalog_active_category']['parent_parent']);
		foreach ($list as $level) {
			$out['text'] .= "<li>";
			$class = "";
			if ($this->uri->segment(1) == $level['parent_curl'] || $this->uri->segment(1) == $level['parent_curl'].".html" || $out['data_from_model']['catalog_active_category']['parent_id'] == $level['parent_id'] || $out['data_from_model']['catalog_active_category']['parent_parent'] == $level['parent_id']) $class='active';
				$out['text'] .= "<a href='/".$level['parent_curl'].".html' class='" . $class . "'>";
					$out['text'] .= $level['parent_title'];
				$out['text'] .= "</a>";


					$check_parents = $this->db->select()->from('catalog_parents')->where('parent_parent', $level['parent_id'])->where('parent_status', '1')->order_by('parent_order', 'asc')->get()->result_array();
					if (sizeof($check_parents) > 0) {
						$out['text'] .= "<ul>";
							foreach ($check_parents as $lparent) {
								$out['text'] .= "<li>";
									if ($this->uri->segment(2) == $lparent['parent_curl'] || $this->uri->segment(2) == $lparent['parent_curl'].".html" || $out['data_from_model']['catalog_active_category']['parent_id'] == $lparent['parent_id']) $lclass='active'; else $lclass = "";
									$out['text'] .= "<a class='".$lclass."' href='/".$level['parent_curl']."/".$lparent['parent_curl'].".html'>".$lparent['parent_title']."</a>";
								$out['text'] .= "</li>";
							}
						$out['text'] .= "</ul>";
					}

					if ($class == "active") {
//						$check_items = $this->db->select()->from('catalog_items')->where('item_parent', $level['parent_id'])->where('item_status', '1')->order_by('item_order', 'asc')->get()->result_array();
//						if (sizeof($check_items) > 0) {
//							$out['text'] .= "<ul>";
//								foreach ($check_items as $litem) {
//									$out['text'] .= "<li>";
//										if ($this->uri->segment(2) == $litem['item_curl'].".html" || "/".$this->uri->segment(1) == $litem['item_curl']) $lclass='active'; else $lclass = "";
//										$out['text'] .= "<a class='".$lclass."' href='".$this->catalog_service_model->get_item_link($litem['item_id'])."'>".$litem['item_title']."</a>";
//									$out['text'] .= "</li>";
//								}
//							$out['text'] .= "</ul>";
//						}
					}


			$out['text'] .= "</li>";
		}
		$out['text'] .= "</ul>";
		$out['show_head'] = true;
		return $out;
	}
	function polls() {
		$this->load->model('modules/polls_model');
		$out['text'] = $this->polls_model->block();
		$out['show_head'] = true;
		if ($this->uri->segment(1) != "polls.html") return $out;
	}
	function news() {
		$out['text'] = $this->news_model->block();
		$out['show_head'] = true;
		return $out;
	}
	function opinions() {
		$out['text'] = $this->catalog_service_model->opinions();
		$out['show_head'] = true;
		return $out;
	}
	function catalog_search() {
		$out['show_head'] = false;
		$out['text'] = $this->load->view('modules/catalog/block', $data, true);
		return $out;
	}
	function just_call() {
		$p = $this->db->select()->from('content')->where('page_curl', 'just_call')->get()->row_array();
		$out['show_head'] = false;
		$out['text'] = htmlspecialchars_decode($p['page_text']);
		return $out;
	}
	function social() {
		$out['text'] = "<!-- Place this tag where you want the +1 button to render -->
		<g:plusone size=\"medium\"></g:plusone>

		<!-- Place this render call where appropriate -->
		<script type=\"text/javascript\">
		  window.___gcfg = {lang: 'ru'};

		  (function() {
			var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
			po.src = 'https://apis.google.com/js/plusone.js';
			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
		  })();
		</script>";
		$out['text'] .= "";
		$out['show_head'] = false;
		return $out;
	}
	function tags($data) {
		if (sizeof($data['category_tags']) > 0) {
			$out['text'] = "";
			$out['text'] = "<ul class='menu-items'>";
			$link = explode("?", $_SERVER['REQUEST_URI']);
				foreach ($data['category_tags'] as $tag) {
				if (urldecode($this->input->get('tag')) == $tag) $class = "active"; else $class = "";
				$out['text'] .= "<li><a href='".$link[0]."?tag=".urlencode($tag)."' class='".$class."'>".$tag."</a></li>";
			}
			$out['text'] .= "</ul>";
			$out['show_head'] = true;
		}
		return $out;
	}

}
?>